//
//  OADLayoutSampleViewController.m
//  OASDK_Works
//
//  Created by ChenJie on 3/15/20.
//  Copyright © 2020 Mist. All rights reserved.
//

#import "OADLayoutSampleViewController.h"
#import "OASDK.h"
#import "OADLayoutControlPanel.h"

@interface OADLayoutSampleViewController ()

@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) OADLayoutControlPanel *controlPanel;

@end

@implementation OADLayoutSampleViewController

#pragma mark - ViewController lifecycle

- (instancetype)init {
    self = [super init];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = NSStringFromClass(self.class);
    
    //
    [self.view addSubview:self.contentView];
    [self.contentView addSubview:self.layoutContainer];
    [self.view addSubview:self.controlPanel];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    //
    CGFloat topMark = 0;
    CGFloat controlPanelTopMargin = 0;
    CGFloat controlPanelHeight = [OADLayoutControlPanel controlPanelHeight];
    
    //
    CGFloat controlPanelTop = self.view.oa_height - controlPanelHeight;
    self.controlPanel.frame = CGRectMake(0, controlPanelTop, self.view.oa_width, controlPanelHeight);
    
    //
    topMark = OA_NAVIGATION_BAR_BOTTOM;
    CGFloat contentHeight = controlPanelTop - controlPanelTopMargin - topMark;
    self.contentView.frame = CGRectMake(0, topMark, self.view.oa_width, contentHeight);
    
    [self applyLayout];
    [self.layoutContainer oa_showBoundsWithAlpha:0.5];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

#pragma mark - Public methods

- (void)updateLayoutWithDirection:(Direction)direction
                   justifyContent:(JustifyContent)justifyContent
                       alignItems:(AlignItems)alignItems {
    
    self.layoutDirection = direction;
    self.layoutJustifyContent = justifyContent;
    self.layoutAlignItems = alignItems;
    self.layout = nil;
    
    [self.layoutContainer applyLayout:self.layout];
}

- (void)setupLayout:(OADLayout5 *)layout {
    
}

#pragma mark - Private methods

- (void)applyLayout {
    [self.layoutContainer applyLayout:self.layout];
}

#pragma mark - Event response methods

- (void)buttonClicked:(UIButton *)button {
    NSLog(@"button clicked...");
}

#pragma mark - Deletate methods



#pragma mark - Setter



#pragma mark - Getter

- (UIView *)contentView {
    if (!_contentView) {
        UIView *view = [UIView new];
        _contentView = view;
    }
    return _contentView;
}

- (OADLayoutControlPanel *)controlPanel {
    if (!_controlPanel) {
        OADLayoutControlPanel *panel = [OADLayoutControlPanel controlPanelWithDirection:self.layoutDirection
                                                                         justifyContent:self.layoutJustifyContent
                                                                             alignItems:self.layoutAlignItems];
        __weak typeof(self) weakSelf = self;
        panel.layoutDidChange = ^(Direction direction, JustifyContent justifyContent, AlignItems alignItems) {
            [weakSelf updateLayoutWithDirection:direction justifyContent:justifyContent alignItems:alignItems];
        };
        _controlPanel = panel;
    }
    return _controlPanel;
}

- (UIView *)layoutContainer {
    if (!_layoutContainer) {
        UIView *view = [UIView new];
        _layoutContainer = view;
    }
    return _layoutContainer;
}

- (OADLayout5 *)layout {
    if (!_layout) {
        OADLayout5 *layout = [OADLayout5 new];
        
        layout.direction = self.layoutDirection;
        layout.justifyContent = self.layoutJustifyContent;
        layout.alignItems = self.layoutAlignItems;
        
        [self setupLayout:layout];
        
        _layout = layout;
    }
    return _layout;
}

#pragma mark - Memory management

- (void)dealloc {
}

@end
